package com.moleq.dialog;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;

import com.moleq.adapter.SelectionAdapter;
import com.moleq.adapter.SelectionAdapter2;
import com.moleq.base.MoleQBaseDialog;
import com.moleq.mgpos.activities.R;
import com.moleq.models.Receipt;

public class SizeJustifyDialog extends MoleQBaseDialog {

	private Button btn_size_cancel,btn_size_ok;
	private ListView lv_dialog_size, lv_dialog_justify;
	private List<Receipt> mDataSource = new  ArrayList<Receipt>();
	private List<List<String>> sizeSource = new ArrayList<List<String>>();
	private List<List<String>> justifySource = new ArrayList<List<String>>();
	private SelectionAdapter2 sizeAdapter;
	private SelectionAdapter2 justifyAdapter;
	private int selectedRow = 0;
	private String size,justify;
	
	private Button.OnClickListener mOnClickListener = new Button.OnClickListener() {

		@Override
		public void onClick(View v) {	
			switch(v.getId()){
			case R.id.btn_size_cancel:
				dismiss();
				break;
			case R.id.btn_size_ok:	
				if(mDataSource.size() <= 0){
					return;	
				}
				
				String size = sizeSource.get(sizeAdapter.GetSelectedIndex()).get(0).toString();
				if (size.equals("Large")) {
					size = "L";
				} else if (size.equals("Normal")) {
					size = "N";
				}
				mDataSource.get(selectedRow).setLarge(size);
				
				String justify = justifySource.get(justifyAdapter.GetSelectedIndex()).get(0).toString();
				if (justify.equals("Left")) {
					justify = "L";
				} else if (justify.equals("Center")) {
					justify = "C";
				} else if (justify.equals("Right")) {
					justify = "R";
				}
				mDataSource.get(selectedRow).setJustify(justify);
				
//				ScrollToRow(selectedRow);
//				mEditableListener.onEdited(mID);
				mDataSource.get(selectedRow).setEdit(true);
				
				onCloseWithReturn(mDataSource);
				dismiss();
				break;
			}
		}
	};
	
	public ListView.OnItemClickListener onItemClickListener = new ListView.OnItemClickListener() {


		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {

			switch (parent.getId()) {
			case R.id.lv_dialog_size:
				sizeAdapter.ScrollToRow(position);
				break;
			case R.id.lv_dialog_justify:
				justifyAdapter.ScrollToRow(position);
				break;
			}
		}
	};
	
	
	public void onCloseWithReturn(Object obj){
		onReturnValue(0,obj);
		dismiss();
	}
	
	public SizeJustifyDialog(Context context) {
		super(context,context);
	}
	
	public SizeJustifyDialog(Context context, Object base) {
		super(context,base);
	}
	
	public SizeJustifyDialog(Context context, int theme) {
		super(context, theme);
	}
	
	public void setSource(List<Receipt> mDataSource,String size,String justify, int row){
		this.mDataSource = mDataSource;
		this.size = size;
		this.justify = justify;
		this.selectedRow = row;
	}
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		// set UI layout
		this.setContentView(R.layout.ps_dialog_size_justify);
		this.setCancelable(false);
	
		btn_size_cancel = (Button) findViewById(R.id.btn_size_cancel);
		btn_size_cancel.setOnClickListener(mOnClickListener);
		
		btn_size_ok = (Button) findViewById(R.id.btn_size_ok);
		btn_size_ok.setOnClickListener(mOnClickListener);

		lv_dialog_size = (ListView) findViewById(R.id.lv_dialog_size);
		lv_dialog_size.setOnItemClickListener(onItemClickListener);
		
		lv_dialog_justify = (ListView) findViewById(R.id.lv_dialog_justify);
		lv_dialog_justify.setOnItemClickListener(onItemClickListener);
		
		sizeAdapter = new SelectionAdapter2(mContext,R.layout.selection_items2,sizeSource);
		lv_dialog_size.setAdapter(sizeAdapter);
		
		justifyAdapter = new SelectionAdapter2(mContext,R.layout.selection_items2,justifySource);
		lv_dialog_justify.setAdapter(justifyAdapter);
	}
	
	public void show(){
		super.show();	
		
		sizeSource.clear();
		justifySource.clear();
		
		List<String> columns = new ArrayList<String>();
		columns.add("Normal");
		sizeSource.add(columns);
		
		columns = new ArrayList<String>();
		columns.add("Large");
		sizeSource.add(columns);
		
		columns = new ArrayList<String>();
		columns.add("Left");
		justifySource.add(columns);
		
		columns = new ArrayList<String>();
		columns.add("Center");
		justifySource.add(columns);
		
		columns = new ArrayList<String>();
		columns.add("Right");
		justifySource.add(columns);
		
		
		
		Locate(size);
		LocateJustify(justify);
	}
	
	public void Locate(String size) {
		for (int i = 0; sizeSource.size() > i; i++) {
			if (sizeSource.get(i).get(0).equals(size)) {
				sizeAdapter.ScrollToRow(i);
				return;
			}
		}
		sizeAdapter.ScrollToRow(0);
	}
	
	public void LocateJustify(String justify) {
		for (int i = 0; justifySource.size() > i; i++) {
			if (justifySource.get(i).get(0).equals(justify)) {
				justifyAdapter.ScrollToRow(i);
				return;
			}
		}
		justifyAdapter.ScrollToRow(0);
	}
}
